/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package com.huawei.rpc.config;

import com.huawei.rpc.client.ClientProxyFactory;
import com.huawei.rpc.client.discovery.ZookeeperServiceDiscoverer;
import com.huawei.rpc.client.network.NettyNetWorkClient;
import com.huawei.rpc.common.processor.DefaultRpcProcessor;
import com.huawei.rpc.common.prorocol.MessageProtocol;
import com.huawei.rpc.server.NettyRpcServer;
import com.huawei.rpc.server.RequestHandler;
import com.huawei.rpc.server.RpcServer;
import com.huawei.rpc.server.register.ServiceRegister;
import com.huawei.rpc.server.register.ZookeeperExportServiceRegister;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Lijl
 * @ClassName AutoConfiguration
 * @Description 自动装配
 * @Date 2022/2/16 9:25
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties({ProtocolProperties.class,RegistryProperties.class})
public class AutoConfiguration {

    private ProtocolProperties protocolProperties;
    private RegistryProperties registryProperties;

    public AutoConfiguration(ProtocolProperties protocolProperties,RegistryProperties registryProperties){
        this.protocolProperties = protocolProperties;
        this.registryProperties = registryProperties;
    }

    @Bean
    public DefaultRpcProcessor defaultRpcProcessor(){
        return new DefaultRpcProcessor();
    }

    /**
     * @Author lijl
     * @MethodName clientProxyFactory
     * @Description 注册客户端动态代理
     * @Date 20:50 2022/2/26
     * @Version 1.0
     * @param
     * @return: com.huawei.rpc.client.ClientProxyFactory
    **/
    @Bean
    public ClientProxyFactory clientProxyFactory(){
        ClientProxyFactory clientProxyFactory = new ClientProxyFactory();
        //设置服务发现者
        String address = registryProperties.getAddress();
        int port = registryProperties.getPort();
        clientProxyFactory.setIDiscovererService(new ZookeeperServiceDiscoverer(address+":"+port));

        //设置支持的协议
        Map<String, MessageProtocol> supportMessageProtocols = new HashMap<>(16);
        supportMessageProtocols.put(protocolProperties.getName(), protocolProperties.getMessageProtocol());
        clientProxyFactory.setSupportMessageProtocols(supportMessageProtocols);

        //设置网络层实现
        clientProxyFactory.setNetWorkClient(new NettyNetWorkClient());
        return clientProxyFactory;
    }

    /**
     * @Author lijiale
     * @MethodName serviceRegister
     * @Description 服务注册
     * @Date 20:52 2022/2/26
     * @Version 1.0
     * @param
     * @return: com.huawei.rpc.server.register.ServiceRegister
    **/
    @Bean
    public ServiceRegister serviceRegister(){
        String address = registryProperties.getAddress();
        int port = registryProperties.getPort();
        return new ZookeeperExportServiceRegister(address+":"+port, protocolProperties.getPort(), protocolProperties.getName());
    }

    @Bean
    public RequestHandler requestHandler(ServiceRegister serviceRegister){
        return new RequestHandler(protocolProperties.getMessageProtocol(), serviceRegister);
    }

    @Bean
    public RpcServer rpcServer(RequestHandler requestHandler){
        return new NettyRpcServer(protocolProperties.getPort(), protocolProperties.getName(), requestHandler);
    }
}
